package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.MediaConstant;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@RocketMQMessageListener(
        consumerGroup = "MediaConsumer",
        topic = MediaConstant.TOPIC_COURSE_VIDEO_PUSH_TO_SRS,
        selectorExpression = MediaConstant.TAGS_COURSE_VIDEO_PUSH_TO_SRS
)
@Slf4j
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService iMediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, "utf-8");
            MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
            JsonResult jsonResult = iMediaFileService.handleFile2m3u8(mediaFile);
            log.info("异步推流结果：{}", jsonResult.getSuccess()? "成功":"失败");
            //进行异步推流
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
